package com.tsd.jxc.goods.service.impl;

import com.tsd.core.thread.ThreadPoolTaskUtil;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.PageBean;
import com.tsd.jxc.goods.dao.JxcGoodsPriceMapper;
import com.tsd.jxc.goods.entity.JxcGoodsExt;
import com.tsd.jxc.goods.entity.JxcGoodsPrice;
import com.tsd.jxc.goods.entity.JxcGoodsPriceExt;
import com.tsd.jxc.goods.service.JxcGoodsPriceService;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.entity.SysUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 描述：JxcGoodsPrice 服务实现层
 *
 * @author david
 * @date 2024/05/06 16:33:50
 */
@Service
public class JxcGoodsPriceServiceImpl extends BaseServiceImpl implements JxcGoodsPriceService {

    @Resource
    private JxcGoodsPriceMapper jxcGoodsPriceMapper;

    @Override
    public JxcGoodsPrice getJxcGoodsPriceById(Long id) throws Exception {
        return jxcGoodsPriceMapper.selectByPrimaryKey(id);
    }

    @Override
    public JxcGoodsPriceExt queryBySid(String sid) throws Exception {
        return jxcGoodsPriceMapper.selectBySid(sid);
    }

    @Override
    public void deleteJxcGoodsPrices(List<String> list, SysUser opUser) throws Exception {
        for (String sid : list) {
            JxcGoodsPrice old = jxcGoodsPriceMapper.selectBySid(sid);
            super.checkEmpty(old, "该记录不存在");
            super.autoInjectBaseData(old, opUser, TYPE_DELETE);
            jxcGoodsPriceMapper.updateByPrimaryKeySelective(old);
        }
    }

    @Override
    public void saveJxcGoodsPrice(JxcGoodsPrice record, SysUser opUser) throws Exception {
        if (HlpUtils.isEmpty(record.getId())) {
            super.checkEmpty(record.getGoods_sid(), "缺少参数：goods_sid");
            super.checkEmpty(record.getUnit_name(), "请输入单位名称");
            super.autoInjectBaseData(record, opUser, TYPE_CREATE);
            jxcGoodsPriceMapper.insertSelective(record);
        } else {
            super.autoInjectBaseData(record, opUser, TYPE_UPDATE);
            jxcGoodsPriceMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    public List<JxcGoodsPriceExt> findJxcGoodsPrices(Map<String, Object> params, PageBean page) throws Exception {
        return jxcGoodsPriceMapper.selectByPage(params, page);
    }

    @Override
    public List<JxcGoodsPriceExt> findJxcGoodsPrices(Map<String, Object> params) throws Exception {
        return jxcGoodsPriceMapper.selectByParams(params);
    }

    @Override
    public void saveJxcGoodsPrice4Goods(JxcGoodsExt record, SysUser opUser) throws Exception {
        List<JxcGoodsPriceExt> priceExts = record.getPrice_list();
        for (JxcGoodsPriceExt priceExt : priceExts) {
            priceExt.setGoods_sid(record.getSid());
            priceExt.calcVolume();
            this.saveJxcGoodsPrice(priceExt, opUser);
        }
        jxcGoodsPriceMapper.deleteNotIn(record);
    }

    @Override
    public List<JxcGoodsPriceExt> queryByGoodsSid(String goods_sid) {
        return jxcGoodsPriceMapper.selectByGoodsSid(goods_sid);
    }

    @Override
    public List<JxcGoodsPriceExt> queryByGoodsList(List<String> goods_list) {
        return ThreadPoolTaskUtil.queryInList4Split(goods_list, itemList -> {
            JxcGoodsPriceExt params = new JxcGoodsPriceExt();
            params.setDr(0);
            params.setGoods_list(itemList);
            return jxcGoodsPriceMapper.selectByEntity(params);
        });
    }
}
